package com.todo.content.infrastructure.repository.impl;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.content.infrastructure.entity.condition.ContentTopicCondition;
import com.todo.content.infrastructure.entity.po.ContentTopicPO;
import com.todo.content.infrastructure.mapper.ContentTopicMapper;
import com.todo.content.infrastructure.repository.ContentTopicRepository;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (ContentTopic)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Repository("contentTopicRepository")
public class ContentTopicRepositoryImpl implements ContentTopicRepository {
    @Resource
    private ContentTopicMapper contentTopicMapper;

    @Override
    public PageResult<ContentTopicPO> queryContentTopicByCondition(ContentTopicCondition contentTopicCondition) {
        PageResult<ContentTopicPO> pageResult = new PageResult<>();
        if (contentTopicCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(contentTopicCondition.getPageIndex());
        pageResult.setPageSize(contentTopicCondition.getPageSize());
        long count = queryCountByCondition(contentTopicCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, contentTopicCondition.getPageSize()));
        pageResult.setTotal(count);
        List<ContentTopicPO> sightPoiPOList = contentTopicMapper.queryContentTopicByCondition(contentTopicCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= contentTopicCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(ContentTopicCondition contentTopicCondition) {
        if (contentTopicCondition == null || contentTopicCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return contentTopicMapper.queryCountByCondition(contentTopicCondition);
    }

    @Override
    public Boolean insertContentTopic(ContentTopicPO contentTopicPO) {
        if (contentTopicPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (contentTopicPO.getName() == null || contentTopicPO.getTopicType() == null || contentTopicPO.getBackendImg() == null) {
            return false;
        }
        if (contentTopicPO.getIsEnable() == null || contentTopicPO.getCreateUser() == null) {
            return false;
        }
        return contentTopicMapper.insertContentTopicPO(contentTopicPO) > 0;
    }

    @Override
    public Boolean updateContentTopicById(ContentTopicPO contentTopicPO) {
        if (contentTopicPO == null || contentTopicPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return contentTopicMapper.updateContentTopicPOById(contentTopicPO) > 0;
    }


    @Override
    public Boolean removeContentTopicById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicPO contentTopicPO = queryContentTopicPOById(id);
        if (contentTopicPO == null) {
            return true;
        }
        return contentTopicMapper.removeContentTopicPOById(id) > 0;
    }

    @Override
    public ContentTopicPO queryContentTopicPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return contentTopicMapper.queryContentTopicPOById(id);
    }

    @Override
    public Boolean updateContentTopicCount(ContentTopicPO contentTopicPO) {
        if (contentTopicPO == null || contentTopicPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return contentTopicMapper.updateContentTopicCount(contentTopicPO);
    }

    @Override
    public ContentTopicPO queryContentTopicByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return contentTopicMapper.queryContentTopicByName(name);
    }

    @Override
    public List<ContentTopicPO> searchContentTopicByName(String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return Lists.newArrayList();
        }
        return contentTopicMapper.searchContentTopicByName(keyword);
    }

    @Override
    public List<ContentTopicPO> queryContentTopicByIdList(List<Long> topicIdList) {
        if (CollectionUtils.isEmpty(topicIdList)) {
            return Collections.emptyList();
        }
        return contentTopicMapper.queryContentTopicByIdList(topicIdList);
    }

    @Override
    public Boolean enableContentTopicById(Long topicId, Integer enable) {
        if (topicId == null || enable == null) {
            return false;
        }
        return contentTopicMapper.enableContentTopicById(topicId, enable);
    }

}
